<?php

namespace App\Controller\LazyCommon;

use App\Base\BaseConfig;
use App\Base\BaseController;
use App\Base\BaseController4User;
use App\Model\LazyCommon\UserModel;
use App\Model\LazyCommon\UserInviteModel;
use App\Model\LazyCommon\MiniAppModel;
use App\Model\LazyCommon\WechatModel;
use App\Model\LazyCommon\WechatPayModel;
use App\Model\LazyCommon\WechatTemplateMessage;
use App\Model\LazyCommon\WechatTemplateMessageModel;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;

/**
 * @route("/wechat")
 */
class TestController extends BaseController4User
{

    private function initData()
    {
        $this->setSessionData(array("role"=>["ROLE_USER"]));
        $this->setSessionData(array("id"=>1));
        return $this->data;
    }


    /**
     * @Route("/testlsd",name="testlsd")
     */
    public function testlsd(Request $request)
    {

        return $this->render('App:test:index.html.twig',$this->data);
    }

    /**
     * @Route("/testmsg",name="WechatMsgTest")
     */
    public function WechatMsgTest(Request $request)
    {
        $Model = new WechatTemplateMessageModel($this->getDoctrine());
//        $Model = new WechatModel($this->getDoctrine());
        $Model->sendTemplateMessage("oQ_Fh1uqeyXRmTweTA2zoTh_sxqg","title","k1","ke2","remark");
        $Model->productSellOut("oQ_Fh1uqeyXRmTweTA2zoTh_sxqg","库存不知","产品1","20","请及时采购");

    }


    /**
     * @Route("/testreg",name="WechatRegTest")
     */
    public function WechatRegTest(Request $request)
    {
        $UserModel = new UserModel($this->getDoctrine());
        $UserInviteModel = new UserInviteModel($this->getDoctrine());
        if ($request->getMethod() == "POST") {
            if(!$key = $this->getParamRequired("key")){die("用户分享码呢？");}
            if(!$act = $this->getParamRequired("act")){return $this->json_out(0,"");}
            switch ($act){
                case "add":
                    //todo 增加用用户
                    $wechat_id = uniqid();
                    $user = $UserModel->getByWeChetId($wechat_id);
                    $userInvite = $UserInviteModel->getUser($user->getId());
                    if (!$userInvite || !$userInvite->getParent()) {
                        //没有邀请记录说明没有绑定过
                        //获取key ,获取上级信息
                        $UserInviteModel->addUser($user->getId(),$key);
                    }
                    break;
                case "red":
                    //todo 从链中删除用户
                    $userInvite = $UserInviteModel->getUserByCode($key);
                    $UserInviteModel->removeUser($userInvite->getUid());
                    break;
            }
//            return $this->redirectToRoute("Wechattestreg");
        }
        //todo 列出所有用户树
        $data = $UserInviteModel->getSonsTree("1",10);

//        var_dump($data);
//        $ids = array();
//        $ids = $UserInviteModel->getSonsIds($data,$ids);
//        var_dump($ids);
//        var_dump($UserModel->getUserFromIds($ids[1]));
        return $this->render('App:test:index.html.twig',$data);
    }

    /**
     * @Route("/testpay",name="WechatPayTest")
     */
    public function WechatPayTest(Request $request)
    {
        $data = $this->initData();

        $WechatModel = new WechatModel($this->getDoctrine());
        if(!$code = $this->getParamRequired("code")){
            $WechatModel->getWeChatCode(BaseConfig::__BASE_API__."/wechat/testpay");
            die;
        }

        $WechatModel->getAccessToken();
        $WechatModel->getJsApiTicket();
        $JsApiparam = $WechatModel->createJsApiParam($request->getUri());

        $WechatPayModel = new WechatPayModel($this->getDoctrine());
        $opend_id = "oySZl1PQeQouIJvsrsgxp4PecCu0";
        $JsParam = $WechatPayModel->createOrder($opend_id,"支付测试BODAY",uniqid(),1);

        $data["p"] = $JsApiparam;
        $data["p1"] = $JsParam;
        return $this->render('App:test:pay.html.twig',$data);
    }


    /**
     * @Route("/testpaymsg",name="WechatPayMsgTest")
     */
    public function WechatPayMsgTest(Request $request)
    {
//        $WechatModel = new WechatModel($this->getDoctrine());
//        if(!$code = $this->getParamRequired("code")){
//            $WechatModel->getWeChatCode(BaseConfig::__BASE_API__."/wechat/testpay");
//            die;
//        }
//        $WechatModel->getAccessToken();
//        var_dump($WechatModel->AccessToken);
//        $WechatModel->getJsApiTicket();
//        $JsApiparam = $WechatModel->createJsApiParam(BaseConfig::__BASE_API__."/wechat/testpay");
//        var_dump($JsApiparam);
//
//        $WechatPayModel = new WechatPayModel($this->getDoctrine());
//        $opend_id = "oySZl1PQeQouIJvsrsgxp4PecCu0";
//        $JsParam = $WechatPayModel->createOrder($opend_id,"支付测试BODAY",uniqid(),1);
//        var_dump($JsParam);
//
//        $data["p"] = $JsApiparam;
//        $data["p1"] = $JsParam;
//        return $this->render('App:test:pay.html.twig',$data);
    }

    /**
     * @Route("/getAccessToken",name="getAccessToken")
     */
    public function getAccessToken()
    {

        $WechatPayModel = new WechatModel($this->getDoctrine());
        $tk = $WechatPayModel->getAccessToken();
        echo $tk;
        die;
    }
    /**
     * @Route("/getCode",name="getCode")
     */
    public function getCode(Request $request)
    {

        $WechatPayModel = new WechatModel($this->getDoctrine());
        $tk = $WechatPayModel->getWeChatCode($request->getUri(),1);
        echo $tk;
        die;
    }


}
